﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao.CommonDao;
using CSP.DataAccess;
using CSP.Model.CSP;
using CSP.Model.CSPEnum;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class CreditLineDao : CSPCommonDao, ICreditLineDao
    {

        public CreditLineDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public ICollection<Model.CSP.CreditLine> FindAll()
        {
            throw new NotImplementedException();
        }

        public Model.CSP.CreditLine GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        public ICollection<Model.CSP.CreditLine> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        public void Add(Model.CSP.CreditLine obj)
        {
            _context.CreditLines.Add(obj);
            _context.SaveChanges();
        }


        public void Update(Model.CSP.CreditLine obj)
        {
            throw new NotImplementedException();
        }

        public Model.CSP.CreditLine DeleteById(int id)
        {
            throw new NotImplementedException();
        }


        public int FindCountByUsersAndOrderNumber(string fromUserName, string toUserName, string orderNumber)
        {
            return _context.CreditLines.Count(c => c.FromUsername == toUserName && c.ToUserName == toUserName && c.OrderNumber == orderNumber);
        }



        public List<int> FindIdsFromConsumeByServiceUser(string serviceUserName)
        {
            List<int> result = new List<int>();
            var temIds = _context.CreditLines.Where(c => c.ToUserName == serviceUserName && c.RatingType == (int)RatingType.Consume).Select(c => c.id);
            if (temIds.Count() != 0)
            {
                result = temIds.ToList();
            }

            return result;
        }

        public List<int> FindIdsFromServiceByConsumeUser(string consumeUserName)
        {
            List<int> result = new List<int>();
            var temIds = _context.CreditLines.Where(c => c.ToUserName == consumeUserName && c.RatingType == (int)RatingType.Service).Select(c => c.id);
            if (temIds.Count() != 0)
            {
                result = temIds.ToList();
            }

            return result;
        }

        public List<int> FindIdsByFromUser(string fromUser)
        {
            List<int> result = new List<int>();
            var temIds = _context.CreditLines.Where(c => c.FromUsername == fromUser).Select(c => c.id);
            if (temIds.Count() != 0)
            {
                result = temIds.ToList();
            }

            return result;
        }

        public List<int> FindIdsFromConsumeByServiceId(int serviceId)
        {
            List<int> result = new List<int>();
            var temIds = _context.CreditLines.Where(c => c.ServiceInfoId == serviceId).Select(c => c.id);
            if (temIds.Count() != 0)
            {
                result = temIds.ToList();
            }

            return result;
        }

        public ICollection<CreditLine> GetCreditLineByIds(List<int> ids, int pageIndex, int pageSize)
        {
            List<CreditLine> result = new List<CreditLine>();
            var creditLines = _context.CreditLines.Where(c => ids.Contains(c.id)).OrderByDescending(c => c.id).Skip(pageIndex * pageSize).Take(pageSize);
            if (creditLines.Count() != 0)
            {
                result = creditLines.ToList();
            }
            return result;
        }

        public List<int> FindIdsByIdsAndContentType(List<int> ids, int contentType)
        {
            List<int> result = new List<int>();

            //内容为空
            if (contentType == 0)
            {
                var temIds = _context.CreditLines.Where(c => ids.Contains(c.id) && (c.RatingContent == null || c.RatingContent == "")).Select(c => c.id);
                if (temIds.Count() != 0)
                {
                    result = temIds.ToList();
                }
            }
            else if (contentType == 1)
            {
                var temIds = _context.CreditLines.Where(c => ids.Contains(c.id) && c.RatingContent != null && c.RatingContent != "").Select(c => c.id);
                if (temIds.Count() != 0)
                {
                    result = temIds.ToList();
                }
            }
            return result;
        }

        public List<int> FindIdsByIdsAndCreditRatingType(List<int> ids, CreditRatingType creditRatingType)
        {
            List<int> result = new List<int>();
            var temIds = _context.CreditLines.Where(c => ids.Contains(c.id) && c.CreditRating == (int)creditRatingType).Select(c => c.id);
            if (temIds.Count() != 0)
            {
                result = temIds.ToList();
            }
            return result;
        }
    }
}
